<template>
  <div>
    <div class="avatar-container" @click="chooseImageAndUpload">
      <div class="avatar-outline">
        <img :src="avatarUrl" :alt="avatarAlt" class="avatar-image" v-if="avatarUrl" />
        <div class="avatar-placeholder" v-else>点击上传图片</div>
      </div>
    </div>
    <div class="divider"></div>
    <view class="container-Password">
      <view class="password-box">
        <view class="title-Password">密</view>
        <view class="title-Password">码：</view>
        <!--  :value="password" -->
        <input v-model="password" class="password" :type="hiddenPassword ? 'password' : 'text'" />
        <image :src="toggleImage" class="toggle-image" @tap="togglePasswordVisibility" />
      </view>
    </view>
    <div class="container-adminID">
      <div class="box-admin">
        <span class="admin-id">工号:</span>
        <span class="adminid-number">{{ adminId }}</span>
      </div>
    </div>
    <div class="container-adminName">
      <div class="box-admin">
        <span class="admin-id">姓名:</span>
        <span class="adminid-number">{{ adminName }}</span>
      </div>
    </div>
    <div class="container">
      <div class="gender-option" @click="showDialog = true">
        <span class="gender-label">性别：</span>{{ selectedGender }}
      </div>
      <div class="dialog" v-if="showDialog">
        <div class="dialog-content">
          <div class="dialog-title">选择性别</div>
          <div class="dialog-options">
            <div class="dialog-option" @click="selectGender('男')">男</div>
            <div class="dialog-divider"></div>
            <div class="dialog-option" @click="selectGender('女')">女</div>
          </div>
        </div>
      </div>
    </div>
    <div>
      <div class="nickname-container" @click="goToNicknameInput">
        <div class="label">号码：</div>
        <div class="nickname" :class="{ 'with-nickname': nickname }">{{ nickname }}</div>
        <div class="placeholder" v-if="!nickname">请输入您的手机号</div>
      </div>
      <div v-if="showNicknameInput" class="input-container">
        <input v-model="newNickname" type="text" placeholder="请输入您的手机号" class="input-field">
        <button @click="submitNickname">提交</button>
      </div>
    </div>
    <button @click="submitForm" class="submitForm-button">修改完成</button>
  </div>
</template>

<script>
  import store from '@/store/index.js' //需要引入store
  // import { mapState } from 'vuex'
  export default {
    data() {
      return {
        showDialog: false,
        selectedGender: '',
        avatarUrl: '',
        avatarAlt: '头像',
        showNicknameInput: false,
        nickname: '', //管理员初始获得的电话号
        newNickname: '', //当初写急了，一切Nickname的意思其实都是手机号,但其实没啥用
        adminId: '', //管理员的工号
        adminName: '', //管理员姓名
        hiddenPassword: true, //是否隐藏密码
        password: '', // 初始密码
        toggleImage: '/static/项目图片/眼睛_隐藏.png', // 切换图片
        defaultImage: 'https://pic.imgdb.cn/item/6497fd0d1ddac507cc3852a2.png', //默认图片
      }
    },

    computed: {
      //显示是否为默认图片的
      displayedImage() {
        return this.avatarUrl || this.defaultImage
      },
      //监测密码的位数，有多少位密码就显示多少个星号
      hidePassword() {
        return '*'.repeat(this.password.length)
      },
    },
    //从后台中获取账号ID的接口书写处
    mounted() {
      this.fetchAdminData() // 获取管理员数据
    },
    methods: {
      //数据渲染到界面上
      fetchAdminData() {
        this.adminId = store.state.admin.Admin_ID,
          this.adminName = store.state.admin.Admin_Name,
          this.selectedGender = store.state.admin.Admin_Sex,
          this.password = store.state.admin.Admin_Password,
          this.avatarUrl = store.state.admin.Admin_image
      },

      //表单数据封装
      submitForm() {
        // 将表单数据封装为一个对象
        const formData = {
          avatarUrl: this.avatarUrl,
          password: this.password,
          adminId: this.adminId,
          adminName: this.adminName,
          selectedGender: this.selectedGender,
          // 其他字段...
        }

        // 在这里可以进行进一步处理或发送到后端
        // 修改管理员信息
        console.log('提交表单数据:', formData)
        uni.request({
          url: 'http://127.0.0.1:3007/api/updateAdmin',
          method: 'POST',
          header: { 'Content-Type': 'application/x-www-form-urlencoded' },
          data: {
            name: formData.adminName,
            password: formData.password,
            image: formData.avatarUrl,
            sex: formData.selectedGender,
            id: formData.adminId
          },
          success: res => {
            console.log('网络请求成功res：', res)
            if (res.data.status === 1) {
              uni.showToast({
                title: res.data.message,
                icon: 'none',
              })
            }
            if (res.data.status === 0) {
              uni.request({
                url: 'http://127.0.0.1:3007/api/loginAdmin',
                method: 'POST',
                header: { 'Content-Type': 'application/x-www-form-urlencoded' },
                data: {
                  name: this.adminName,
                  password: this.password
                },
                success: res => {
                  if (res.data.status === 0) {
                    store.commit('loginAdmin', res.data.data.admin)
                  }
                  console.log('修改信息后adminInfo', store.state.admin)
                },
                fail: error => {
                  console.error(error)
                  uni.showToast({ title: '网络请求失败' })
                }
              })
              uni.showToast({ title: '修改成功' })
            }
          },
          fail: error => {
            console.error(error)
            uni.showToast({ title: '网络请求失败' })
          }
        })
      },
      //这是密码栏目的控制效果
      togglePasswordVisibility() {
        this.hiddenPassword = !this.hiddenPassword
        this.toggleImage = this.hiddenPassword ? '/static/项目图片/眼睛_隐藏.png' : '/static/项目图片/眼睛_显示.png'
      },
      //页面跳转控制已经寄了2020/6/19
      // goToRePassword() {
      //       uni.navigateTo({
      //         url: '/pages/index/RePassword',
      //       });
      //     },
      //下面是性别的控制
      selectGender(gender) {
        this.selectedGender = gender
        this.showDialog = false
        // 将选中的性别发送到后台或进行其他操作
        // 在这里添加你的逻辑代码
      },
      chooseImageAndUpload() {
        wx.chooseImage({
          count: 1,
          sourceType: ['album', 'camera'],
          success: res => {
            const tempFilePaths = res.tempFilePaths
            const tempFilePath = tempFilePaths[0]
            wx.compressImage({
              src: tempFilePath,
              quality: 80,
              success: res => {
                const compressedFilePath = res.tempFilePath
                this.avatarUrl = compressedFilePath
              },
              fail: error => {
                console.error('压缩图片失败：', error)
              },
            })
          },
          fail: error => {
            console.error('选择图片失败：', error)
          },
        })
      },
      goToNicknameInput() {
        this.showNicknameInput = true
      },
      submitNickname() {
        // 将新昵称保存到后端数据库等操作
        // 更新昵称
        this.nickname = this.newNickname
        // 返回昵称显示界面
        this.showNicknameInput = false
      },
    },
  }
</script>

<style scoped>
  .avatar-container {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .avatar-outline {
    width: 100px;
    height: 100px;
    border: 2px solid #f6d800;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .avatar-image {
    width: 100%;
    /* 设置固定宽度 */
    height: 100%;
    /* 设置固定高度 */
    object-fit: cover;
    border-radius: 50%;
  }

  .avatar-placeholder {
    font-size: 14px;
    color: #333;
  }

  .divider {
    height: 1px;
    background-color: #f6d800;
    margin: 10px auto;
    width: 80%;
  }

  .container {
    margin-top: 20px;
    margin-bottom: 10px;
    display: flex;
    justify-content: center;
  }

  .gender-option {
    padding: 10px;
    background-color: #fff;
    border: 2px solid #0000ff;
    border-radius: 5px;
    cursor: pointer;
    width: 80%;
    text-align: center;
    display: flex;
    align-items: center;
  }

  .gender-label {
    font-size: 20px;
    font-weight: bold;
    margin-right: 5px;
    color: #000;
  }

  .dialog {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 999;
    backdrop-filter: blur(5px);
    /* 设置模糊程度，可以根据需要进行调整 */
    background-color: rgba(255, 255, 255, 0.8);
    /* 设置背景颜色和透明度，以便与模糊效果结合 */
  }

  .dialog-content {
    padding: 20px;
    border: 2px solid #ffcc00;
    border-radius: 5px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  }

  .dialog-title {
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 10px;
    color: #000;
  }

  .dialog-options {
    display: flex;
    flex-direction: column;
  }

  .dialog-option {
    padding: 10px;
    cursor: pointer;
    color: #000;
  }

  .dialog-divider {
    height: 1px;
    background-color: #c2e5f6;
    margin: 10px 0;
  }

  .nickname-container {
    display: flex;
    align-items: center;
    padding: 10px;
    border: 2px solid #007bff;
    border-radius: 5px;
    width: 80%;
    /* 调整点击框宽度 */
    background-color: #fff;
    margin: 0 auto;
    cursor: pointer;
  }

  .label {
    font-size: 20px;
    font-weight: bold;
    margin-right: 5px;
    color: #000;
  }

  .nickname {
    padding: 10px;
    margin-left: 5px;
    transition: transform 0.3s ease;
  }

  .nickname.with-nickname {
    transform: translateX(-5px);
  }

  .placeholder {
    color: #ccc;
    font-size: 14px;
    margin-left: 10px;
  }

  .input-container {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px;
    width: 90%;
    margin: 10px auto;
  }

  .input-field {
    flex: 1;
    padding: 5px;
    border: 1px solid #007bff;
    border-radius: 5px;
    height: 50px;
    /* 调整输入框高度 */
  }

  .input-container button {
    padding: 5px 10px;
    background-color: #000000;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    margin-left: 8px;
    height: 50px;
  }

  /*以下为管理员的工号框和姓名框的样式设计*/
  .container-adminID {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }

  .container-adminName {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }

  .box-admin {
    background-color: #fff;
    width: 73%;
    max-width: 500px;
    border: 2px solid blue;
    padding: 20px;
    display: flex;
    align-items: center;
    border-radius: 5px;
    height: 8px;
    margin-left: -2px;
    margin-bottom: -8px;
  }

  .admin-id {
    font-size: 20px;
    font-weight: bold;
    margin-right: 10px;
    color: #000;
    position: absolute;
    left: 30px;
    /* 调整"工号"文字的左侧位置 */
  }

  .adminid-number {
    color: #ffcccc;
    /* 淡红色 */
    margin-left: 54px;
  }

  /*密码框的样式调整*/
  .container-Password {
    background-color: #fff;
    justify-content: center;
    width: 87%;
    height: 52px;
    margin-left: 20px;
    margin-bottom: -10px;
    margin-top: 30px;
  }

  .password-box {
    display: flex;
    align-items: center;
    border-radius: 8px;
    border: 2px solid blue;
    padding: 10px;
    height: 30px;
  }

  .title-Password {
    margin-right: 5px;
    text-align: center;
    font-weight: bold;
    text-size: 22px;
  }

  .password {
    flex-grow: 1;
    padding: 10px;
    cursor: pointer;
    margin-left: -12px;
  }

  .hidden-password {
    font-size: 16px;
    position: relative;
    /* 添加定位属性 */
    top: 3px;
    /* 根据需要调整位置 */
  }

  .visible-password {
    font-size: 16px;
  }

  .toggle-image {
    width: 24px;
    height: 24px;
    cursor: pointer;
  }

  .submitForm-button {
    border: 2px solid greenyellow;
    background-color: skyblue;
    color: white;
    padding: 10px 20px;
    border-radius: 10px;
    text-align: center;
    margin-top: 20px;
    width: 60%;
  }
</style>